﻿using DualReferralServer.Entity;
using DualReferralServer.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;

namespace DualReferralServer.Service
{
    public class DualReferralService
    {
        DualReferralModel dualReferralModel = new DualReferralModel();

        //一级科室查询
        public string GetDepartment(string xml)
        {
            string resultParam = "";
            string messageParam = "";
            string msgidParam = "";
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            string yydm = doc.SelectSingleNode("/request/yydm").InnerText;
            string ywbs = doc.SelectSingleNode("/request/ywbs").InnerText;
            if (!yydm.Equals("43120477700"))
            {
                resultParam = "false";
                messageParam = "医院代码输入有误";
                msgidParam = "1";

            }
            else if (!ywbs.Equals("ZY"))
            {
                resultParam = "false";
                messageParam = "输入类型错误";
                msgidParam = "2";
            }
            else
            {
                resultParam = "true";
                messageParam = "查询成功";
                msgidParam = "0";
            }


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("response");
            xmldoc.AppendChild(xmlRoot);
            //交易结果
            XmlElement result = xmldoc.CreateElement("result");
            XmlText resultText = xmldoc.CreateTextNode(resultParam);
            result.AppendChild(resultText);
            xmlRoot.AppendChild(result);
            //消息
            XmlElement message = xmldoc.CreateElement("message");
            XmlText messageText = xmldoc.CreateTextNode(messageParam);
            message.AppendChild(messageText);
            xmlRoot.AppendChild(message);
            //消息编码
            XmlElement msgid = xmldoc.CreateElement("msgid");
            XmlText msgidText = xmldoc.CreateTextNode(msgidParam);
            msgid.AppendChild(msgidText);
            xmlRoot.AppendChild(msgid);
            //消息主体
            if (msgidParam.Equals("0"))
            {
                List<Department_standard> list = dualReferralModel.GetDepartment_Standards();
                foreach (Department_standard department_standard in list)
                {
                    XmlElement department = xmldoc.CreateElement("department");
                    XmlElement bzksdm = xmldoc.CreateElement("bzksdm");
                    XmlText bzksdmText = xmldoc.CreateTextNode(department_standard.bzksdm);
                    bzksdm.AppendChild(bzksdmText);
                    department.AppendChild(bzksdm);
                    XmlElement bzksmc = xmldoc.CreateElement("bzksmc");
                    XmlText bzksmcText = xmldoc.CreateTextNode(department_standard.bzksmc);
                    bzksmc.AppendChild(bzksmcText);
                    department.AppendChild(bzksmc);
                    xmlRoot.AppendChild(department);
                }
            }
            return xmldoc.InnerXml;
        }

        //二级科室查询
        public string GetDepartmentlevel2(string xml)
        {
            string resultParam = "";
            string messageParam = "";
            string msgidParam = "";
            string bzksdmParam = "";
            var doc = new XmlDocument();
            doc.LoadXml(xml);


            try
            {
                bzksdmParam = doc.SelectSingleNode("/request/bzksdm").InnerText;
            }
            catch (NullReferenceException e)
            {
                Trace.WriteLine("用户未传一级科室代码");
            }

            string yydm = doc.SelectSingleNode("/request/yydm").InnerText;
            string ywbs = doc.SelectSingleNode("/request/ywbs").InnerText;

            if (!yydm.Equals("43120477700"))
            {
                resultParam = "false";
                messageParam = "医院代码输入有误";
                msgidParam = "1";

            }
            else if (!ywbs.Equals("ZY"))
            {
                resultParam = "false";
                messageParam = "输入类型错误";
                msgidParam = "2";
            }
            else
            {
                resultParam = "true";
                messageParam = "查询成功";
                msgidParam = "0";
            }


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("response");
            xmldoc.AppendChild(xmlRoot);
            //交易结果
            XmlElement result = xmldoc.CreateElement("result");
            XmlText resultText = xmldoc.CreateTextNode(resultParam);
            result.AppendChild(resultText);
            xmlRoot.AppendChild(result);
            //消息
            XmlElement message = xmldoc.CreateElement("message");
            XmlText messageText = xmldoc.CreateTextNode(messageParam);
            message.AppendChild(messageText);
            xmlRoot.AppendChild(message);
            //消息编码
            XmlElement msgid = xmldoc.CreateElement("msgid");
            XmlText msgidText = xmldoc.CreateTextNode(msgidParam);
            msgid.AppendChild(msgidText);
            xmlRoot.AppendChild(msgid);
            //消息主体
            if (msgidParam.Equals("0"))
            {
                List<Department_hospital> list = dualReferralModel.GetDepartment_Hospitals(bzksdmParam);
                foreach (Department_hospital department_Hospital in list)
                {
                    XmlElement department = xmldoc.CreateElement("department");
                    XmlElement bzksdm = xmldoc.CreateElement("bzksdm");
                    XmlText bzksdmText = xmldoc.CreateTextNode(department_Hospital.bzksdm);
                    bzksdm.AppendChild(bzksdmText);
                    department.AppendChild(bzksdm);
                    XmlElement bzksmc = xmldoc.CreateElement("bzksmc");
                    XmlText bzksmcText = xmldoc.CreateTextNode(department_Hospital.bzksmc);
                    bzksmc.AppendChild(bzksmcText);
                    department.AppendChild(bzksmc);
                    XmlElement ksdm = xmldoc.CreateElement("ksdm");
                    XmlText ksdmText = xmldoc.CreateTextNode(department_Hospital.ksdm);
                    ksdm.AppendChild(ksdmText);
                    department.AppendChild(ksdm);
                    XmlElement ksmc = xmldoc.CreateElement("ksmc");
                    XmlText ksmcText = xmldoc.CreateTextNode(department_Hospital.ksmc);
                    ksmc.AppendChild(ksmcText);
                    department.AppendChild(ksmc);
                    XmlElement jzdz = xmldoc.CreateElement("jzdz");
                    XmlText jzdzText = xmldoc.CreateTextNode(department_Hospital.jzdz);
                    jzdz.AppendChild(jzdzText);
                    department.AppendChild(jzdz);
                    xmlRoot.AppendChild(department);
                }
            }
            return xmldoc.InnerXml;
        }

        //医师查询
        public string GetDoctors(string xml)
        {
            string resultParam = "";
            string messageParam = "";
            string msgidParam = "";
            string ksdmParam = "";
            string ysghParam = "";
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            string yydm = doc.SelectSingleNode("/request/yydm").InnerText;
            string ywbs = doc.SelectSingleNode("/request/ywbs").InnerText;

            try
            {
                ksdmParam = doc.SelectSingleNode("/request/ksdm").InnerText;
                ysghParam = doc.SelectSingleNode("/request/ysgh").InnerText;
            }
            catch (NullReferenceException e)
            {
                Trace.WriteLine("用户未传科室代码或医师工号");
            }

            if (!yydm.Equals("43120477700"))
            {
                resultParam = "false";
                messageParam = "医院代码输入有误";
                msgidParam = "1";

            }
            else if (!ywbs.Equals("ZY"))
            {
                resultParam = "false";
                messageParam = "输入类型错误";
                msgidParam = "2";
            }
            else
            {
                resultParam = "true";
                messageParam = "查询成功";
                msgidParam = "0";
            }


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("response");
            xmldoc.AppendChild(xmlRoot);
            //交易结果
            XmlElement result = xmldoc.CreateElement("result");
            XmlText resultText = xmldoc.CreateTextNode(resultParam);
            result.AppendChild(resultText);
            xmlRoot.AppendChild(result);
            //消息
            XmlElement message = xmldoc.CreateElement("message");
            XmlText messageText = xmldoc.CreateTextNode(messageParam);
            message.AppendChild(messageText);
            xmlRoot.AppendChild(message);
            //消息编码
            XmlElement msgid = xmldoc.CreateElement("msgid");
            XmlText msgidText = xmldoc.CreateTextNode(msgidParam);
            msgid.AppendChild(msgidText);
            xmlRoot.AppendChild(msgid);
            //消息主体
            if (msgidParam.Equals("0"))
            {
                List<Doctor> list = dualReferralModel.GetDoctors(ksdmParam, ysghParam);
                foreach (Doctor doctors in list)
                {
                    XmlElement doctor = xmldoc.CreateElement("doctor");
                    XmlElement ksdm = xmldoc.CreateElement("ksdm");
                    XmlText ksdmText = xmldoc.CreateTextNode(doctors.ksdm);
                    ksdm.AppendChild(ksdmText);
                    doctor.AppendChild(ksdm);
                    XmlElement ksmc = xmldoc.CreateElement("ksmc");
                    XmlText bzksmcText = xmldoc.CreateTextNode(doctors.ksmc);
                    ksmc.AppendChild(bzksmcText);
                    doctor.AppendChild(ksmc);
                    xmlRoot.AppendChild(doctor);
                    XmlElement ysgh = xmldoc.CreateElement("ysgh");
                    XmlText ysghText = xmldoc.CreateTextNode(doctors.ysgh);
                    ysgh.AppendChild(ysghText);
                    doctor.AppendChild(ysgh);
                    XmlElement ysxm = xmldoc.CreateElement("ysxm");
                    XmlText ysxmText = xmldoc.CreateTextNode(doctors.ysxm);
                    ysxm.AppendChild(ysxmText);
                    doctor.AppendChild(ysxm);
                }
            }
            return xmldoc.InnerXml;
        }

        //床位统计查询
        public string GetBedNums(string xml)
        {
            string resultParam = "";
            string messageParam = "";
            string msgidParam = "";
            string ksdmParam = "";
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            string yydm = doc.SelectSingleNode("/request/yydm").InnerText;

            try
            {
                ksdmParam = doc.SelectSingleNode("/request/ksdm").InnerText;
            }
            catch (NullReferenceException e)
            {
                Trace.WriteLine("用户未传科室代码");
            }

            if (!yydm.Equals("43120477700"))
            {
                resultParam = "false";
                messageParam = "医院代码输入有误";
                msgidParam = "1";

            }
            else
            {
                resultParam = "true";
                messageParam = "查询成功";
                msgidParam = "0";
            }


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("response");
            xmldoc.AppendChild(xmlRoot);
            //交易结果
            XmlElement result = xmldoc.CreateElement("result");
            XmlText resultText = xmldoc.CreateTextNode(resultParam);
            result.AppendChild(resultText);
            xmlRoot.AppendChild(result);
            //消息
            XmlElement message = xmldoc.CreateElement("message");
            XmlText messageText = xmldoc.CreateTextNode(messageParam);
            message.AppendChild(messageText);
            xmlRoot.AppendChild(message);
            //消息编码
            XmlElement msgid = xmldoc.CreateElement("msgid");
            XmlText msgidText = xmldoc.CreateTextNode(msgidParam);
            msgid.AppendChild(msgidText);
            xmlRoot.AppendChild(msgid);
            //消息主体
            if (msgidParam.Equals("0"))
            {
                List<Bed_statistics> list = dualReferralModel.GetBed_Statistics(ksdmParam);
                foreach (Bed_statistics beds in list)
                {
                    XmlElement bed = xmldoc.CreateElement("bed");
                    XmlElement ksdm = xmldoc.CreateElement("ksdm");
                    XmlText ksdmText = xmldoc.CreateTextNode(beds.ksdm);
                    ksdm.AppendChild(ksdmText);
                    bed.AppendChild(ksdm);
                    XmlElement ksmc = xmldoc.CreateElement("ksmc");
                    XmlText bzksmcText = xmldoc.CreateTextNode(beds.ksmc.Trim());
                    ksmc.AppendChild(bzksmcText);
                    bed.AppendChild(ksmc);
                    xmlRoot.AppendChild(bed);
                    XmlElement cwzs = xmldoc.CreateElement("cwzs");
                    XmlText cwzsText = xmldoc.CreateTextNode(beds.cwzs.ToString());
                    cwzs.AppendChild(cwzsText);
                    bed.AppendChild(cwzs);
                    XmlElement zyzs = xmldoc.CreateElement("zyzs");
                    XmlText zyzsText = xmldoc.CreateTextNode(beds.zyzs.ToString());
                    zyzs.AppendChild(zyzsText);
                    bed.AppendChild(zyzs);
                }
            }
            return xmldoc.InnerXml;
        }

        //床位实时查询
        public string GetBedRealTime(string xml)
        {
            string resultParam = "";
            string messageParam = "";
            string msgidParam = "";
            string ksdmParam = "";
            var doc = new XmlDocument();
            doc.LoadXml(xml);

            string yydm = doc.SelectSingleNode("/request/yydm").InnerText;

            try
            {
                ksdmParam = doc.SelectSingleNode("/request/ksdm").InnerText;
            }
            catch (NullReferenceException e)
            {
                Trace.WriteLine("用户未传科室代码");
            }

            if (!yydm.Equals("43120477700"))
            {
                resultParam = "false";
                messageParam = "医院代码输入有误";
                msgidParam = "1";

            }
            else
            {
                resultParam = "true";
                messageParam = "查询成功";
                msgidParam = "0";
            }


            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("response");
            xmldoc.AppendChild(xmlRoot);
            //交易结果
            XmlElement result = xmldoc.CreateElement("result");
            XmlText resultText = xmldoc.CreateTextNode(resultParam);
            result.AppendChild(resultText);
            xmlRoot.AppendChild(result);
            //消息
            XmlElement message = xmldoc.CreateElement("message");
            XmlText messageText = xmldoc.CreateTextNode(messageParam);
            message.AppendChild(messageText);
            xmlRoot.AppendChild(message);
            //消息编码
            XmlElement msgid = xmldoc.CreateElement("msgid");
            XmlText msgidText = xmldoc.CreateTextNode(msgidParam);
            msgid.AppendChild(msgidText);
            xmlRoot.AppendChild(msgid);
            //消息主体
            if (msgidParam.Equals("0"))
            {
                List<Bed_realtime> list = dualReferralModel.GetBed_Realtimes(ksdmParam);
                foreach (Bed_realtime beds in list)
                {
                    XmlElement bed = xmldoc.CreateElement("bed");
                    XmlElement cwdm = xmldoc.CreateElement("cwdm");
                    XmlText cwdmText = xmldoc.CreateTextNode(beds.cwdm);
                    cwdm.AppendChild(cwdmText);
                    bed.AppendChild(cwdm);
                    XmlElement sfzy = xmldoc.CreateElement("sfzy");
                    XmlText sfzyText = xmldoc.CreateTextNode(beds.sfzy);
                    sfzy.AppendChild(sfzyText);
                    bed.AppendChild(sfzy);
                    xmlRoot.AppendChild(bed);
                    XmlElement ryrq = xmldoc.CreateElement("ryrq");
                    XmlText ryrqText = xmldoc.CreateTextNode(beds.ryrq);
                    ryrq.AppendChild(ryrqText);
                    bed.AppendChild(ryrq);
                    XmlElement cwdz = xmldoc.CreateElement("cwdz");
                    XmlText cwdzText = xmldoc.CreateTextNode(beds.cwdz);
                    cwdz.AppendChild(cwdzText);
                    bed.AppendChild(cwdz);
                }
            }
            return xmldoc.InnerXml;
        }
    }
}